<template>
  <view class="login-container">
    <!-- 背景图轮播 -->
    <view class="background-slider">
      <image
        v-for="(bg, index) in backgroundImages"
        :key="index"
        :src="url('g', index , 'png')"
        class="background-image"
        :class="{
          active: currentBgIndex === index,
          'fade-out': isChanging && currentBgIndex === index,
          'fade-in': isChanging && nextBgIndex === index,
        }"
      />
    </view>

    <!-- 遮罩层 -->
    <view class="overlay"></view>

    <!-- 修改密码卡片 -->
    <view class="password-card animate__animated animate__fadeIn">
      <view class="card-header">
        <uni-icons type="locked" size="24" color="#666"></uni-icons>
        <text class="header-title">{{ status.data.message }}</text>
      </view>

      <uni-forms :modelValue="formData" ref="formRef">
        <uni-forms-item name="email">
          <uni-easyinput
            v-model="formData.email"
            placeholder="邮箱"
            prefixIcon="email"
          />
        </uni-forms-item>

        <uni-forms-item name="account">
          <uni-easyinput
            v-model="formData.account"
            placeholder="手机号"
            prefixIcon="person"
          />
        </uni-forms-item>

        <uni-forms-item name="password">
          <uni-easyinput
            v-model="formData.password"
            type="password"
            placeholder="新密码"
            prefixIcon="locked"
          />
        </uni-forms-item>

        <button class="submit-btn" @tap="handleSubmit">
          <uni-icons type="refresh" size="20" color="#fff"></uni-icons>
          更新密码
        </button>
      </uni-forms>

      <view class="loading-section" v-if="theloading">
        <uni-load-more status="loading" :content-text="loadingText" />
      </view>

      <!-- <view class="tips-section">
        <text class="tips-text">
          DCloud <uni-icons type="cloud-upload" size="16" /> 云函数保障账号安全
        </text>
      </view> -->

      <view class="register-link">
        <text>没有账号？</text>
        <navigator url="/pages/other/register" class="link-text"
          >注册一个! 
        </navigator>
      </view>
    </view>
  </view>
</template>

<script setup>
import { reactive, ref, onMounted, onUnmounted } from "vue";
const url = $url;
const theloading = ref(false);
const formRef = ref(null);

// 修改表单数据结构
const formData = reactive({
  password: "",
  email: "13800138001@163.com",
  account: "",
});

const status = ref({
  data: {
    message: "修改密码",
  },
});

const loadingText = {
  contentdown: "加载中...",
  contentrefresh: "请稍候...",
  contentnomore: "",
};

// 背景图切换逻辑
const backgroundImages = [
  "/static/bg1.jpg",
  "/static/bg2.jpg",
  "/static/bg3.jpg",
];
const currentBgIndex = ref(0);
const nextBgIndex = ref(0);
const isChanging = ref(false);
let bgTimer = null;

// 切换背景图
const changeBg = () => {
  isChanging.value = true;
  nextBgIndex.value = (currentBgIndex.value + 1) % backgroundImages.length;

  setTimeout(() => {
    currentBgIndex.value = nextBgIndex.value;
    isChanging.value = false;
  }, 1000);
};

// 修改提交处理函数名称和实现
const handleSubmit = async () => {
  // 表单验证

  if (!formData.account || !formData.password || !formData.email) {
    uni.showToast({
      title: "请填写完整信息",
      icon: "none",
    });
    return;
  }

  // 手机号验证
  if (!/^1[3-9]\d{9}$/.test(formData.account)) {
    uni.showToast({
      title: "请输入正确的手机号",
      icon: "none",
    });
    return;
  }

  // 邮箱验证
  if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(formData.email)) {
    uni.showToast({
      title: "请输入正确的邮箱",
      icon: "none",
    });
    return;
  }

  theloading.value = true;

  try {
    const res = await uni.request({
      url: "https://fc-mp-d7d620be-76ea-43bb-a05a-d38ceb13c655.next.bspapp.com/updatapassword",
      method: "POST",
      data: {
        account: formData.account,
        password: formData.password,
        email: formData.email,
      },
    });

    status.value = res;
    const index = res.data.status;
    console.log(res);

    switch (index) {
      case 1:
        uni.showToast({
          title: "密码更新成功",
          icon: "success",
          success: () => {
            setTimeout(() => {
              uni.navigateTo({
                url: "/pages/other/login",
              });
            }, 1500);
          },
        });
        break;
      case -2:
        uni.showToast({
          title: "账号或邮箱不正确",
          icon: "error",
        });
        break;
      default:
        uni.showToast({
          title: res.data.message || "更新失败",
          icon: "error",
        });
    }
  } catch (error) {
    uni.showToast({
      title: "更新失败",
      icon: "error",
    });
  } finally {
    theloading.value = false;
  }
};

onMounted(() => {
  bgTimer = setInterval(changeBg, 5000);
});

onUnmounted(() => {
  if (bgTimer) clearInterval(bgTimer);
});
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.background-slider {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 1s ease-in-out;

  &.active {
    opacity: 1;
  }

  &.fade-out {
    opacity: 0;
  }

  &.fade-in {
    opacity: 1;
  }
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}

.password-card {
  position: relative;
  width: 90%;
  max-width: 600rpx;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20rpx;
  padding: 40rpx;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.2);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10rpx;
  margin-bottom: 40rpx;
}

.header-title {
  font-size: 36rpx;
  color: #333;
}

.submit-btn {
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  background: var(--td-brand-color, #0052d9);
  color: #fff;
  border-radius: 40rpx;
  font-size: 32rpx;
  margin-top: 40rpx;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10rpx;

  &:active {
    opacity: 0.8;
  }
}

.tips-section {
  text-align: center;
  margin: 30rpx 0;
}

.tips-text {
  font-size: 24rpx;
  color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6rpx;
}

.register-link {
  text-align: center;
  font-size: 28rpx;
}

.link-text {
  color: var(--td-brand-color, #0052d9);
  display: inline-block;
  margin-left: 10rpx;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.animate__fadeIn {
  animation: fadeIn 0.8s ease-out;
}
</style>
